/*
 * @copyright fxsen <br/>
 * @date 2022/3/30 15:54 <br/>
 * @email <a href="mailto:fxsen@foxmail.com">
 * @author fxsen
 */
package com.fxsen.commons.utils;

import com.fxsen.commons.core.base.common.entity.JwtUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * @author fxsen
 * @date 2022/3/30 15:54
 * @email <a href="mailto:fxsen@foxmail.com">
 */
public class ContextUtils {
    public static String getCurrentUserId(){
        try {
            return JwtUtils.getParamByToken(getToken()).getUserId();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    public static String getCurrentUserId(HttpServletRequest request){
        try {
            return JwtUtils.getParamByToken(getToken(request)).getUserId();
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    public static JwtUser getCurrentUser(){
        try {
            return JwtUtils.getParamByToken(getToken());
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    public static JwtUser getCurrentUser(HttpServletRequest request){
        try {
            return JwtUtils.getParamByToken(getToken(request));
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
    private static String getToken(HttpServletRequest request){
        // 从请求头中取出 token  这里需要和前端约定好把jwt放到请求头一个叫Authorization的地方
        String token = request.getHeader(JwtUtils.JWT_HEADER_KEY);
        //默认全部检查
        if (StringUtils.isBlank(token)){
            // 如果令牌为空, 再取Cookie中X-Token
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (JwtUtils.JWT_HEADER_KEY.equals(cookie.getName())) {
                        token = cookie.getValue();
                        break;
                    }
                }
            }
        }
        if (StringUtils.isBlank(token)){
            token = (String) request.getSession().getAttribute(JwtUtils.JWT_HEADER_KEY);
        }
        if (StringUtils.isBlank(token)){
            // 如果令牌为空, 再取QueryString中token
            token = request.getParameter(JwtUtils.JWT_HEADER_KEY);
        }
        return token;
    }
    private static String getToken(){
        try {
            ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            RequestContextHolder.setRequestAttributes(servletRequestAttributes,true);//设置子线程共享
            if (null != servletRequestAttributes) {
                HttpServletRequest request = servletRequestAttributes.getRequest();
                return getToken(request);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
